package ddbserver.optimizer;

import java.util.HashSet;
import java.util.Vector;

/**
*
* @author LH
* global join node: Predicates like "a.xxx=b.xxx"
* local join node: (1) Name denoting the global table
* 					(2) Attributes denoting join attributes
* union node: (1) Predicates storing horizontal predicates
* 			  (2) Predicate #i is mapped to child #i
* select node: Predicates like "a.xxx < yyy".
* 				Each predicate only concerns one table
* project node: Attributes needed
*/

public class TreeNode {
	private TreeNode parent;
	private String type; //join project select union table
	private String name; //table names
	private Vector<TreeNode> children;
	private HashSet<String> tables; //tables related to the join
	private Vector<String> attrs; //attributes used in projection
	private Vector<String> predicates; //attributes used in selection
	
	public TreeNode() {
		children = new Vector<TreeNode>();
		parent = null;
		type = "";
		name = "";
		tables = new HashSet<String>();
		attrs = new Vector<String>();
		predicates = new Vector<String>();
	}
	
	public void addChild(TreeNode child) {
		children.add(child);
	}
	
	public void setChild(Vector<TreeNode> children) {
		this.children = children;
	}
	
	public Vector<TreeNode> getChild() {
		return children;
	}
	
	public void setParent(TreeNode parent) {
		this.parent = parent;
	}
	
	public TreeNode getParent() {
		return parent;
	}
	
	public void setType(String type) {
		this.type = type;
	}
	
	public String getType() {
		return type;
	} 
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getName() {
		return name;
	}
	
	public void addJoinTable(String tablename) {
		tables.add(tablename);
	}
	
	public boolean isJoinTable(String tablename) {
		return tables.contains(tablename);
	}
	
	public HashSet<String> getJoinTable() {
		return tables;
	}
	
	public void addAttribute(String e) {
		attrs.add(e);
	}
	
	public void setAttribute(Vector<String> attributes) {
		this.attrs = attributes;
	}
	
	public Vector<String> getAttribute() {
		return attrs;
	}
	
	public void addPredicate(String e) {
		predicates.add(e);
	}
	
	public void setPredicates(Vector<String> predicates) {
		this.predicates = predicates;
	}
	
	public Vector<String> getPredicate() {
		return predicates;
	}
	
	public boolean isLeaf() {
		return children.isEmpty();
	}
	
	public TreeNode clone() {
		TreeNode one = new TreeNode();
		for(String item : this.tables) {
			one.addJoinTable(item);
		}
		for(String item : this.attrs) {
			one.addAttribute(item);
		}
		for(String item:this.predicates) {
			one.addPredicate(item);
		}
		for(TreeNode item : this.children) {
			one.addChild(item);
		}
		one.setName(this.name);
		one.setType(this.type);
		one.setParent(this.parent);
		return one;
	}
}
